package com.qixiaobao.web.controller.insurefull;

import com.qixiaobao.common.annotation.Log;
import com.qixiaobao.common.annotation.RepeatSubmit;
import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.enums.BusinessType;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.insure.domain.dto.InsureDTO;
import com.qixiaobao.insure.service.IInsureService;
import com.qixiaobao.web.controller.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 保单存档
 * @Author yangxm
 * @Date 2025/7/2 11:21
 */
@RequestMapping("/insure/full")
@RestController
public class InsureFullController extends BaseController {

    @Autowired
    private IInsureService insureService;

    /**
     * @Description 保单存档列表
     * @Author yangxm
     * @Date 2025/7/2 11:22
     * @param insureDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo list(InsureDTO insureDTO){
        insureDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        return getDataTable(insureService.selectFullList(insureDTO));
    }

    /**
     * @Description 保单存档列表导出
     * @Author yangxm
     * @Date 2025/7/2 11:42
     * @param insureDTO
     * @param response
     */
    @GetMapping("/export")
    public void export(InsureDTO insureDTO, HttpServletResponse response){
        insureDTO.setNoPermissionSupplyIdList(checkPermission());
        insureService.fullExport(insureDTO, response);
    }

    /**
     * @Description 保单存档--原始保单文件批量删除
     * 1.若页面勾选，则优先根据页面勾选的删除。
     * 2.若页面未勾选，则根据查询条件删除，供应商简称必选
     * @Author yangxm
     * @Date 2025/7/2 11:42
     * @param insureDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "保单存档", businessType = BusinessType.REMOVE_ALL_FILE)
    @GetMapping("/removeOriginalInsureFile")
    public AjaxResult removeOriginalInsureFile(InsureDTO insureDTO){
       return toAjax(insureService.removeOriginalInsureFile(insureDTO));
    }

    /**
     * @Description 批量下载原始保单文件
     * @Author yangxm
     * @Date 2025/7/2 13:46
     * @param insureDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/downloadOriginalInsureFile")
    public AjaxResult downloadOriginalInsureFile(InsureDTO insureDTO){
        insureService.downloadOriginalInsureFile(insureDTO);
        return AjaxResult.success();
    }

    /**
     * @Description 原始保单批量上传 - 校验文件
     * @Author yangxm
     * @Date 2025/7/2 14:32
     * @param fileNameList
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/checkUploadOriginalInsureFile")
    public AjaxResult checkUploadOriginalInsureFile(List<String> fileNameList){
        if (StringUtils.isEmpty(fileNameList)) {
            throw new UtilException("请求参数异常，未找到文件名列表");
        }
        fileNameList = fileNameList.stream().map(String::trim).collect(Collectors.toList());
        Map<String, List<String>> results = insureService.checkUploadOriginalInsureFile(fileNameList);
        return AjaxResult.success(results);
    }

    /**
     * @Description 原始保单批量上传
     * @Author yangxm
     * @Date 2025/7/2 14:40
     * @param files
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @PostMapping("/uploadOriginalInsureFile")
    public AjaxResult uploadOriginalInsureFile(MultipartFile[] files){
        if (files == null || files.length == 0) {
            throw new UtilException("上传文件不能为空");
        }
        String message = insureService.uploadOriginalInsureFiles(files);
        return AjaxResult.success(message);
    }
}
